#include"Stack.h"

void StackInit(ST* ps)
{
	ps->_a = NULL;
	ps->_capacity = ps->_top = 0;
}

void StackPush(ST* ps, STDataType data)
{
	if (ps->_top == ps->_capacity)
	{
		int newcapacity = ps->_capacity == 0 ? 4 : ps->_capacity * 2;
		ST* tmp = (ST*)realloc(ps->_a, sizeof(ST) * newcapacity);
		if (tmp == NULL)
		{
			perror("realloc fail");
			exit(-1);
		}
		ps->_a = tmp;
		ps->_capacity = newcapacity;
	}
	
	ps->_a[ps->_top] = data;
	ps->_top++;
}

void StackPop(ST* ps)
{
	assert(ps);
	ps->_top--;
}

STDataType StackTop(ST* ps)
{
	assert(ps);
	return ps->_a[ps->_top - 1];
}

int StackSize(ST* ps)
{
	assert(ps);
	return ps->_top;
}

bool StackEmpty(ST* ps)
{
	assert(ps);
	return ps->_top == 0;
}

void StackDestroy(ST* ps)
{
	assert(ps);
	free(ps->_a);
	ps->_a = NULL;
	ps->_capacity = ps->_top = 0;

}